<template>
  <div class="customer-pickcheck-container">
    <vab-query-form>
      <vab-query-form-left-panel :span="24">
        <el-form :inline="true" :model="queryForm" @submit.prevent>
          <el-space wrap :size="size">
            <el-form-item label="挑入申请查询">
              <el-input
                v-model.trim="queryForm.searchKey"
                clearable
                placeholder="按客户名称"
              />
            </el-form-item>
            <el-form-item>
              <el-input
                v-model.trim="queryForm.pickStaff"
                clearable
                placeholder="按申请人"
              />
            </el-form-item>
            <el-form-item>
              <el-date-picker
                v-model="queryForm.ApplyDate"
                style="width: 240px"
                type="daterange"
                placeholder="按申请时间"
                start-placeholder="申请时间"
                end-placeholder="申请时间"
                value-format="YYYY-MM-DD"
              />
            </el-form-item>
            <el-form-item>
              <el-date-picker
                v-model="queryForm.CheckDate"
                style="width: 240px"
                type="daterange"
                placeholder="按审核时间"
                start-placeholder="审核时间"
                end-placeholder="审核时间"
                value-format="YYYY-MM-DD"
              />
            </el-form-item>
            <el-form-item>
              <el-button :icon="Search" type="primary" @click="queryData">
                查询
              </el-button>
            </el-form-item>
          </el-space>
        </el-form>
      </vab-query-form-left-panel>
    </vab-query-form>
    <el-tabs
      v-model="queryForm.checkState"
      type="border-card"
      @tab-click="handleTabClick"
    >
      <el-tab-pane
        v-for="(item, index) in tabList"
        :key="index"
        :label="item.label"
        :name="item.name"
      >
        <el-table v-loading="listLoading" :data="list">
          <el-table-column
            align="center"
            label="申请ID"
            prop="PickApplyID"
            show-overflow-tooltip
            width="70"
          />
          <el-table-column
            align="center"
            label="申请人"
            show-overflow-tooltip
            width="120"
          >
            <template #default="{ row }">
              <div v-for="(itm, idx) in row.PickStaffCompany" :key="idx">
                {{ itm.CompanyName }}
              </div>
              <div v-for="(itm, idx) in row.PickStaffDeptment" :key="idx">
                <span style="font-size: 10px; color: #999">
                  {{ itm.DepartmentName }}
                </span>
              </div>
              <div>{{ row.PickStaff }}</div>
              <div>{{ row.PickStaffCode }}</div>
            </template>
          </el-table-column>
          <el-table-column
            align="center"
            label="申请时间"
            show-overflow-tooltip
            width="120"
          >
            <template #default="{ row }">
              {{ row.AddTime.split(' ')[0] }}
              <br />
              {{ row.AddTime.split(' ')[1] }}
              <!-- <div style="white-space: normal; word-break: break-all">
                {{ row.AddTime }}
              </div> -->
            </template>
          </el-table-column>
          <el-table-column
            align="center"
            label="客户ID"
            prop="CustomerID"
            show-overflow-tooltip
            width="80"
          />
          <el-table-column
            align="left"
            label="客户名称"
            prop="CustomerAllName"
            show-overflow-tooltip
          />
          <el-table-column align="center" label="商机名称" width="135">
            <template #default="{ row }">
              <el-text size="small">{{ row.BusinessOrderName }}</el-text>
              <el-button
                type="primary"
                size="small"
                plain
                @click="showBusinessPath(row.OrderID)"
              >
                商机路径
              </el-button>
            </template>
          </el-table-column>
          <el-table-column
            align="center"
            label="原商机ID"
            prop="OrderID"
            show-overflow-tooltip
            width="80"
          />
          <el-table-column
            align="center"
            label="原挑入时间"
            show-overflow-tooltip
            width="120"
          >
            <template #default="{ row }">
              {{ row.OrderAddTime.split(' ')[0] }}
              <br />
              {{ row.OrderAddTime.split(' ')[1] }}
              <!-- <div style="white-space: normal; word-break: break-all">
                {{ row.OrderAddTime }}
              </div> -->
            </template>
          </el-table-column>
          <el-table-column
            align="center"
            label="原跟进人"
            show-overflow-tooltip
            width="120"
          >
            <template #default="{ row }">
              <div v-for="(itm, idx) in row.SaleStaffCompany" :key="idx">
                {{ itm.CompanyName }}
              </div>
              <div v-for="(itm, idx) in row.SaleStaffDeptment" :key="idx">
                <span style="font-size: 10px; color: #999">
                  {{ itm.DepartmentName }}
                </span>
              </div>
              <div>{{ row.SaleStaffName }}</div>
              <div>{{ row.SaleStaffCode }}</div>
            </template>
          </el-table-column>
          <el-table-column
            align="left"
            label="申请原因"
            prop="PickMemo"
            show-overflow-tooltip
          />
          <el-table-column align="center" label="审核状态" width="90">
            <template #default="{ row }">
              <div v-if="row.CheckState === 0" style="color: #ff6600">
                待审核
              </div>
              <div v-if="row.CheckState === 1" style="color: green">已通过</div>
              <div v-if="row.CheckState === -1">未通过</div>
            </template>
          </el-table-column>
          <el-table-column
            v-if="item.name !== 0"
            align="center"
            label="审核时间"
            show-overflow-tooltip
            width="105"
          >
            <template #default="{ row }">
              {{ row.CheckTime.split(' ')[0] }}
              <br />
              {{ row.CheckTime.split(' ')[1] }}
              <!-- <div style="white-space: normal; word-break: break-all">
                {{ row.CheckTime.replace(' ','<br>') }}
              </div> -->
            </template>
          </el-table-column>
          <el-table-column
            v-if="item.name !== 0"
            align="left"
            label="审核备注"
            prop="CheckMemo"
            show-overflow-tooltip
          />
          <el-table-column
            v-if="actstaffpostid == 2 || actstaffpostid == 6"
            align="center"
            label="操作"
            width="160"
          >
            <template #default="{ row }">
              <el-button
                type="primary"
                size="small"
                @click="getCustomerTrack(row)"
              >
                查看小记
              </el-button>
              <el-button
                type="primary"
                size="small"
                @click="handleEdit(row, 1)"
              >
                审核
              </el-button>
              <!-- <el-button text @click="handleDelete(row)">删除</el-button> -->
            </template>
          </el-table-column>
          <el-table-column v-else align="center" label="操作" width="160">
            <template #default="{ row }">
              <el-button
                type="primary"
                size="small"
                @click="handleEdit(row, 0)"
              >
                查看申请
              </el-button>
            </template>
          </el-table-column>
        </el-table>
      </el-tab-pane>
    </el-tabs>

    <el-pagination
      background
      :current-page="queryForm.pageNo"
      :layout="layout"
      :page-size="queryForm.pageSize"
      :total="total"
      @current-change="handleCurrentChange"
      @size-change="handleSizeChange"
    />
    <edit ref="editRef" @fetch-data="fetchData" />
    <business-Path ref="BusinessPathRef" @fetch-data="fetchData" />
    <TrackRecord ref="recordRef" @fetch-data="fetchData" />
  </div>
</template>

<script>
  import { PickCheckList, doDelete } from '@/api/customerPickcheck'
  import TrackRecord from '@/views/business/components/TrackRecord'
  import Edit from './components/CustomerPickcheckEdit'
  import BusinessPath from '@/views/components/BusinessPath'
  import { Delete, Plus, Search } from '@element-plus/icons-vue'

  export default defineComponent({
    name: 'CustomerPickcheck',
    components: { Edit, TrackRecord, BusinessPath },
    setup() {
      // const router = useRouter()
      const $baseConfirm = inject('$baseConfirm')
      const $baseMessage = inject('$baseMessage')

      const state = reactive({
        actstaffpostid: 0,
        recordRef: null,
        tabList: [
          {
            label: '新申请待审核',
            name: 0,
          },
          {
            label: '已审核通过',
            name: 1,
          },
          {
            label: '未通过审核',
            name: -1,
          },
        ],
        editRef: null,
        list: [],
        listLoading: true,
        layout: 'total, sizes, prev, pager, next, jumper',
        total: 0,
        selectRows: '',
        queryForm: {
          checkState: 0,
          pageNo: 1,
          pageSize: 10,
          searchKey: '',
          pickStaff: '',
        },
        BusinessPathRef: null,
      })

      const getCustomerTrack = (row) => {
        const order = {
          CustomerPickID: row.CustomerPickID,
          OrderID: row.OrderID,
          TrackClass: [0],
        }
        state.recordRef.handleEdit(order)
      }

      const setSelectRows = (val) => {
        state.selectRows = val
      }
      const handleEdit = (row, n) => {
        if (row.PickApplyID) {
          state.editRef.showEdit(row, n)
        } else {
          state.editRef.showEdit()
        }
      }
      const handleDelete = (row) => {
        if (row.id) {
          $baseConfirm('你确定要删除当前项吗', null, async () => {
            const { msg } = await doDelete({ ids: row.id })
            $baseMessage(msg, 'success', 'vab-hey-message-success')
            await fetchData()
          })
        } else {
          if (state.selectRows.length > 0) {
            const ids = state.selectRows.map((item) => item.id).join()
            $baseConfirm('你确定要删除选中项吗', null, async () => {
              const { msg } = await doDelete({ ids })
              $baseMessage(msg, 'success', 'vab-hey-message-success')
              await fetchData()
            })
          } else {
            $baseMessage('未选中任何行', 'error', 'vab-hey-message-error')
          }
        }
      }
      const handleSizeChange = (val) => {
        state.queryForm.pageSize = val
        fetchData()
      }
      const handleCurrentChange = (val) => {
        state.queryForm.pageNo = val
        fetchData()
      }
      const queryData = () => {
        state.queryForm.pageNo = 1
        fetchData()
      }
      const fetchData = async () => {
        state.listLoading = true
        const {
          data: { list, total },
        } = await PickCheckList(state.queryForm)
        state.list = list
        state.total = total
        state.listLoading = false
      }
      onMounted(() => {
        const userInfo = JSON.parse(localStorage.getItem('userInfo'))
        const actstaffpost = JSON.parse(userInfo.StaffPostID)
        state.actstaffpostid = actstaffpost[0][1]

        fetchData()
      })

      const handleTabClick = (tab) => {
        state.queryForm.checkState = tab.props.name
        fetchData()
      }
      const showBusinessPath = (businessPath) => {
        state.BusinessPathRef.showEdit(businessPath)
      }

      return {
        ...toRefs(state),
        getCustomerTrack,
        setSelectRows,
        handleTabClick,
        handleEdit,
        handleDelete,
        handleSizeChange,
        handleCurrentChange,
        queryData,
        fetchData,
        showBusinessPath,
        Delete,
        Plus,
        Search,
      }
    },
  })
</script>
